
package org.ceteca.explica.client.partners;

import java.util.HashSet;

import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.client.gui.ExplicaDetailEntityView;
import org.ceteca.explica.client.gui.ExplicaExtensionFieldsView;
import org.ceteca.explica.client.gui.ExplicaGUIBuilder;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.CurrencyOT;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.common.PriceListOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.partners.AddressOT;
import org.ceteca.explica.core.partners.BankAccountOT;
import org.ceteca.explica.core.partners.ContactOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.partners.InterfacePartnersErrors;
import org.ceteca.explica.core.partners.PartnerGroupOT;
import org.ceteca.explica.core.partners.PartnerOT;
import org.ceteca.explica.core.partners.PaymentMethodOT;
import org.ceteca.explica.core.partners.RappelTypeOT;
import org.ceteca.explica.core.partners.VendorOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.util.Validator;
import org.ceteca.explica.core.warehouse.InterfaceWarehouse;
import org.ceteca.javaglade.InterfaceGladeGtk;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;

/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class PartnerClient extends CommonClient {

	// WIDGETS
	private Text entryPartnerCode, entryPartnerName, entryPartnerBrandName,
			entryPartnerIdNumber, entryPartnerPhone1, entryPartnerPhone2,
			entryPartnerEmail, entryPartnerFax, entryPartnerPartnerGroup,
			entryPartnerRappelType, entryPartnerVendor,
			entryPartnerPaymentMethod, entryPartnerCurrency,
			entryPartnerCopies, entryPartnerMaxRisk, entryPartnerCurrentRisk,
			entryPriceList, entryPromptPaymentDiscount, entryFixedDiscount;
	private Text entryVersion;

	private Combo comboVatRegime;
	
	private Button
			buttonPartnerIdNumberCompute, buttonPriceList;

	private Button buttonPartnerGroup, buttonRappelType, buttonVendor,
			buttonPaymentMethod, buttonCurrency;

	private TabFolder tabFolder;

	private Tree extCatalog;

	private Composite panelAddress, panelContacts, PanelBankAccounts;

	private ExplicaDetailEntityView viewContacts, viewAddress,
			viewBankAccounts;

	public PartnerClient() {
		super(PartnerOT.class,
				InterfacePartners.EXPLICA_EDIT_PARTNER_GLADE_PATH,
				InterfacePartners.EDIT_PARTNER_DIALOG_NAME);

	}

	@Override
	public BaseVO getEntityData() {

		PartnerOT entity = new PartnerOT();
		if (this.entEditView != null) {
			entity.setName(entryPartnerName.getText());
			entity.setBrandName(entryPartnerBrandName.getText());
			entity.setPhone1(entryPartnerPhone1.getText());
			entity.setPhone2(entryPartnerPhone2.getText());
			entity.setIdNumber(entryPartnerIdNumber.getText());
			entity.setFax(entryPartnerFax.getText());
			entity.setEmail(entryPartnerEmail.getText());
			entity.setPartnerGroup((PartnerGroupOT) entryPartnerPartnerGroup
					.getData());
			entity.setRappelType((RappelTypeOT) entryPartnerRappelType
					.getData());
			entity.setVendor((VendorOT) entryPartnerVendor.getData());
			entity.setPaymentMethod((PaymentMethodOT) entryPartnerPaymentMethod
					.getData());
			entity.setCurrency((CurrencyOT) entryPartnerCurrency.getData());

			entity.setPriceList((PriceListOT) entryPriceList.getData());

			entity.setVatRegime(comboVatRegime.getSelectionIndex());
			entity.setNumCopies(Converter.getInstance().parseInteger(
					entryPartnerCopies.getText()));
			entity.setCurrentRisk(Converter.getInstance().parseBigDecimal(
					entryPartnerCurrentRisk.getText()));
			entity.setMaxRisk(Converter.getInstance().parseBigDecimal(
					entryPartnerMaxRisk.getText()));

			if (entity.getContactSet() == null)
				entity.setContactSet(new HashSet<ContactOT>());

			entity.getContactSet().clear();
			entity.getContactSet().addAll(viewContacts.getRegistries());

			if (entity.getBankAccountSet() == null)
				entity.setBankAccountSet(new HashSet<BankAccountOT>());
			entity.getBankAccountSet().clear();
			entity.getBankAccountSet().addAll(viewBankAccounts.getRegistries());

			if (entity.getAddressSet() == null)
				entity.setAddressSet(new HashSet<AddressOT>());
			entity.getAddressSet().clear();
			entity.getAddressSet().addAll(viewAddress.getRegistries());


			entity.setFixedDiscount(Converter.getInstance().parseDouble(entryFixedDiscount.getText()));
			entity.setPromptPaymentDiscount(Converter.getInstance().parseDouble(entryPromptPaymentDiscount.getText()));
	
			
			if (selectedEntity != null) {
				entity.setVersion(((PartnerOT) this.selectedEntity)
						.getVersion());
				entity.setIdPartner(((PartnerOT) this.selectedEntity)
						.getIdPartner());
			}

		}
		return entity;

	}

	public void getWidgets() {

		int allOk = 0;
		// Entries
		entryPartnerCode = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_CODE_WIDGETNAME);
		if (entryPartnerCode == null)
			allOk = 1;
		entryPartnerName = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_NAME_WIDGETNAME);
		if (entryPartnerName == null)
			allOk = 2;
		entryPartnerBrandName = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_BRANDNAME_WIDGETNAME);
		if (entryPartnerBrandName == null)
			allOk = 3;
		entryPartnerIdNumber = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_IDNUMBER_WIDGETNAME);
		if (entryPartnerIdNumber == null)
			allOk = 4;
		entryPartnerPhone1 = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_PHONE1_WIDGETNAME);
		if (entryPartnerPhone1 == null)
			allOk = 5;
		entryPartnerPhone2 = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_PHONE2_WIDGETNAME);
		if (entryPartnerPhone2 == null)
			allOk = 6;
		entryPartnerEmail = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_EMAIL_WIDGETNAME);
		if (entryPartnerEmail == null)
			allOk = 7;
		entryPartnerFax = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_FAX_WIDGETNAME);
		if (entryPartnerFax == null)
			allOk = 8;
		entryPartnerPartnerGroup = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_PARTNERGROUP_WIDGETNAME);
		if (entryPartnerPartnerGroup == null)
			allOk = 9;
		entryPartnerRappelType = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_RAPPELTYPE_WIDGETNAME);
		if (entryPartnerRappelType == null)
			allOk = 10;
		entryPartnerVendor = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_VENDOR_WIDGETNAME);
		if (entryPartnerVendor == null)
			allOk = 11;
		entryPartnerPaymentMethod = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_PAYMENTMETHOD_WIDGETNAME);
		if (entryPartnerPaymentMethod == null)
			allOk = 12;
		entryPartnerCurrency = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_CURRENCY_WIDGETNAME);
		if (entryPartnerCurrency == null)
			allOk = 13;
		entryPartnerCopies = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_COPIES_WIDGETNAME);
		if (entryPartnerCopies == null)
			allOk = 14;
		entryPartnerMaxRisk = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_MAXRISK_WIDGETNAME);
		if (entryPartnerMaxRisk == null)
			allOk = 15;

		entryPartnerCurrentRisk = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_CURRENTRISK_WIDGETNAME);
		if (entryPartnerCurrentRisk == null)
			allOk = 16;

		entryPriceList = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_PRICELIST_WIDGETNAME);
		if (entryPriceList == null)
			allOk = 17;

		if ((entryVersion = ((Text) this.getWidget(
				InterfacePartners.EDIT_VERSION_WIDGETNAME))) == null) {
			allOk = 18;
			this.logger.error("Error getting entry Version Widget:"
					+ InterfacePartners.EDIT_VERSION_WIDGETNAME);

		}

		comboVatRegime = (Combo) this
				.getWidget(
						InterfacePartners.EDIT_PARTNER_VAT_REGIME_WIDGETNAME);
		if (comboVatRegime == null)
			allOk = 19;

		buttonPartnerIdNumberCompute = (Button) this.getWidget(
				InterfacePartners.EDIT_PARTNER_COMPUTEIDNUMBER_WIDGETNAME);
		if (buttonPartnerIdNumberCompute == null)
			allOk = 20;

		// Selection buttons

		buttonPartnerGroup = (Button) this
				.getWidget(
						InterfacePartners.EDIT_PARTNER_SELECT_PARTNERGROUP_BUTTON_WIDGETNAME);
		if (buttonPartnerGroup == null)
			allOk = 21;

		buttonCurrency = (Button) this
				.getWidget(
						InterfacePartners.EDIT_PARTNER_SELECT_CURRENCY_BUTTON_WIDGETNAME);
		if (buttonCurrency == null)
			allOk = 22;

		buttonPaymentMethod = (Button) this
				.getWidget(
						InterfacePartners.EDIT_PARTNER_SELECT_PAYMENTMETHOD_BUTTON_WIDGETNAME);
		if (buttonPaymentMethod == null)
			allOk = 23;

		buttonRappelType = (Button) this
				.getWidget(
						InterfacePartners.EDIT_PARTNER_SELECT_RAPPELTYPE_BUTTON_WIDGETNAME);
		if (buttonRappelType == null)
			allOk = 24;

		buttonVendor = (Button) this.getWidget(
				InterfacePartners.EDIT_PARTNER_SELECT_VENDOR_BUTTON_WIDGETNAME);
		if (buttonVendor == null)
			allOk = 25;

		buttonPriceList = (Button) this
				.getWidget(
						InterfacePartners.EDIT_PARTNER_SELECT_PRICELIST_BUTTON_WIDGETNAME);
		if (buttonPriceList == null)
			allOk = 26;

		// Panel for build multi selections
		panelAddress = ((Composite) this.getWidget(
				InterfacePartners.EDIT_PARTNER_ADDRESS_PANEL_WIDGETNAME));
		if (panelAddress == null)
			allOk = 27;

		panelContacts = ((Composite) this.getWidget(
				InterfacePartners.EDIT_PARTNER_CONTACT_PANEL_WIDGETNAME));
		if (panelContacts == null)
			allOk = 28;

		PanelBankAccounts = ((Composite) this.getWidget(
				InterfacePartners.EDIT_PARTNER_BANKACCOUNT_PANEL_WIDGETNAME));
		if (PanelBankAccounts == null)
			allOk = 29;

		// MultiSelection entity view
		if (this.entEditView != null) { // totavia no ha sido construido el entity
			// edit dialog
			viewContacts = this.entEditView
					.getMultiSelectEntityView(InterfacePartners.EXPLICA_ENTITY_CONTACT_ID);
			viewAddress = this.entEditView
					.getMultiSelectEntityView(InterfacePartners.EXPLICA_ENTITY_ADDRESS_ID);
			viewBankAccounts = this.entEditView
					.getMultiSelectEntityView(InterfacePartners.EXPLICA_ENTITY_BANKACCOUNT_ID);
			/*
			 * if (viewContacts == null || viewAddress == null ||
			 * viewBankAccounts == null) allOk = false;
			 */
		}

		tabFolder = (TabFolder) this
				.getWidget(InterfacePartners.EDIT_PARTNER_NOTEBOOK_WIDGETNAME);
		if (tabFolder == null)
			allOk = 30;

		extCatalog = (Tree) this.getWidget(
				InterfaceExplica.EXPLICA_ENTITY_EXTENSION_CATALOG_NAME);

		if (extCatalog == null)
			allOk = 31;

		entryPromptPaymentDiscount = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_PROMPTPAYMENTDISCOUNT_WIDGETNAME);
		if (entryPromptPaymentDiscount == null)
			allOk = 32;
		
		entryFixedDiscount = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_FIXEDDISCOUNT_WIDGETNAME);
		if (entryFixedDiscount == null)
			allOk = 33;
		
		if (allOk != 0) {
			// this.logger.fatal("Error getting widgets");
			this.logger.debug("Error getting widget #" + allOk);
			fireExceptionAndExit("Error obtentiendo widgets");
			// System.exit(-1);
		}

	}

	public void setWidgetsValues(BaseVO ent) {

		PartnerOT client = (PartnerOT) ent;

		entryPartnerCode.setText(Converter.getInstance().toString(
				client.getIdPartner()));
		entryPartnerName.setText(client.getName() == null ? "" : client
				.getName());
		entryPartnerBrandName.setText(client.getBrandName() == null ? ""
				: client.getBrandName());
		entryPartnerIdNumber.setText(client.getIdNumber() == null ? "" : client
				.getIdNumber());
		entryPartnerPhone1.setText(client.getPhone1() == null ? "" : client
				.getPhone1());
		entryPartnerPhone2.setText(client.getPhone2() == null ? "" : client
				.getPhone2());
		entryPartnerFax.setText(client.getFax() == null ? "" : client.getFax());
		entryPartnerEmail.setText(client.getEmail() == null ? "" : client
				.getEmail());
		entryPartnerMaxRisk.setText(Converter.getInstance().toString(
				client.getMaxRisk()));
		entryPartnerCurrentRisk.setText(Converter.getInstance().toString(
				client.getCurrentRisk()));
		entryPartnerCopies.setText(Converter.getInstance().toString(
				client.getNumCopies()));

		entryPromptPaymentDiscount.setText(Converter.getInstance().toString(
				client.getPromptPaymentDiscount()));
		entryFixedDiscount.setText(Converter.getInstance().toString(
				client.getFixedDiscount()));
		
		entryVersion.setText(Converter.getInstance().toString(
				client.getVersion()));
		
		if (client.getVatRegime()!=null)
			comboVatRegime.select(client.getVatRegime());
		else
			comboVatRegime.select(0);



		
		
		if (extFieldsView!=null)
			extFieldsView.clear();
		
		if (client.getPartnerGroup() != null)
			if (client.getPartnerGroup().getExtTable()!=null){
				extFieldsView.setExtTable(client.getPartnerGroup().getExtTable());
				fillEntityExtensionTable(client.getIdPartner());
				
			}
			
		
				
		
		
		
		
		// Selection buttons
		if (client.getPartnerGroup() != null) {
			entryPartnerPartnerGroup.setText(client.getPartnerGroup()
					.getDescription());
			entryPartnerPartnerGroup.setData(client.getPartnerGroup());
		}

		if (client.getRappelType() != null) {
			entryPartnerRappelType.setText(client.getRappelType()
					.getDescription());
			entryPartnerRappelType.setData(client.getRappelType());
		}

		if (client.getVendor() != null) {
			entryPartnerVendor.setText(client.getVendor().getName());
			entryPartnerVendor.setData(client.getVendor());
		}

		if (client.getPaymentMethod() != null) {
			entryPartnerPaymentMethod.setText(client.getPaymentMethod()
					.getDescription());
			entryPartnerPaymentMethod.setData(client.getPaymentMethod());
		}

		if (client.getCurrency() != null) {
			entryPartnerCurrency.setText(client.getCurrency().getDescription());
			entryPartnerCurrency.setData(client.getCurrency());
		}

		if (client.getPriceList() != null) {
			entryPriceList.setText(client.getPriceList().getDescription());
			entryPriceList.setData(client.getPriceList());
		}

		// MultiSelection
		if (viewContacts != null)
			viewContacts.setRegistries(client.getContactSet());
		if (viewAddress != null)
			viewAddress.setRegistries(client.getAddressSet());
		if (viewBankAccounts != null)
			viewBankAccounts.setRegistries(client.getBankAccountSet());

	}

	
	@Override
	public OperationResult validateEntity() {
		OperationResult result = new OperationResult();
		boolean datosValidos = true;
		PartnerOT entity = (PartnerOT) getEntityData();

		if (!Validator.getInstance().isValidSpanishNif(entity.getIdNumber())
				&& !Validator.getInstance().isValidSpanishCif(
						entity.getIdNumber())) {
			result
					.addResultMessage(
							OperationResult.RESULT_ERROR,
							ClientContext
									.getInstance()
									.getMsgCatalog()
									.newResultMessage(
											InterfacePartnersErrors.MSG_PARTNERS_NIF_VALIDATION_ERROR));

			datosValidos = false;
		}

		if (entity.getEmail() != null && !entity.getEmail().isEmpty()
				&& !Validator.getInstance().isValidEmail((entity.getEmail()))) {

			result
					.addResultMessage(
							OperationResult.RESULT_ERROR,
							ClientContext
									.getInstance()
									.getMsgCatalog()
									.newResultMessage(
											InterfacePartnersErrors.MSG_PARTNERS_EMAIL_VALIDATION_ERROR));

			datosValidos = false;
		}
		if (entity.getPhone1() != null && !entity.getPhone1().isEmpty()
				&& !Validator.getInstance().isValidPhone((entity.getPhone1()))) {

			result
					.addResultMessage(
							OperationResult.RESULT_ERROR,
							ClientContext
									.getInstance()
									.getMsgCatalog()
									.newResultMessage(
											InterfacePartnersErrors.MSG_PARTNERS_PHONE_VALIDATION_ERROR));

			datosValidos = false;
		}
		if (entity.getPhone2() != null && !entity.getPhone2().isEmpty()
				&& !Validator.getInstance().isValidPhone((entity.getPhone2()))) {

			result
					.addResultMessage(
							OperationResult.RESULT_ERROR,
							ClientContext
									.getInstance()
									.getMsgCatalog()
									.newResultMessage(
											InterfacePartnersErrors.MSG_PARTNERS_PHONE_VALIDATION_ERROR));

			datosValidos = false;
		}

		if (entity.getFax() != null && !entity.getFax().isEmpty()
				&& !Validator.getInstance().isValidPhone((entity.getFax()))) {

			result
					.addResultMessage(
							OperationResult.RESULT_ERROR,
							ClientContext
									.getInstance()
									.getMsgCatalog()
									.newResultMessage(
											InterfacePartnersErrors.MSG_PARTNERS_PHONE_VALIDATION_ERROR));

			datosValidos = false;
		}
		
		
		if (entity.getCurrentRisk().doubleValue()<0 )
		{
			result
			.addResultMessage(
					OperationResult.RESULT_ERROR,
					ClientContext
							.getInstance()
							.getMsgCatalog()
							.newResultMessage(
									InterfacePartnersErrors.MSG_PARTNERS_RISK_VALIDATION_ERROR));

	datosValidos = false;
		}
		
		
		
		if (entity.getFixedDiscount()<0 || entity.getFixedDiscount()>100 || entity.getPromptPaymentDiscount()<0 || entity.getPromptPaymentDiscount()>100){
			result
			.addResultMessage(
					OperationResult.RESULT_ERROR,
					ClientContext
							.getInstance()
							.getMsgCatalog()
							.newResultMessage(
									InterfacePartnersErrors.MSG_PARTNERS_DISCOUNT_VALIDATION_ERROR));

	datosValidos = false;
		}		
		
		

		if (datosValidos)
			result.addResultMessage(OperationResult.RESULT_OK, ClientContext
					.getInstance().getMsgCatalog().newResultMessage(
							InterfacePartnersErrors.MSG_PARTNERS_VALIDATE_OK));
		
		return result;
	}

	@Override
	public void setEditionMode(boolean editionMode) {
		getWidgets();
		entryVersion.setEnabled(false);
		entryPartnerCode.setEnabled(false);
		
		if (entryPartnerCode.getText().equals("0") || entryPartnerCode.getText().isEmpty() ){
			panelAddress.setEnabled(false);
			panelContacts.setEnabled(false);
			PanelBankAccounts.setEnabled(false);
		} else {
			panelAddress.setEnabled(editionMode);
			panelContacts.setEnabled(editionMode);
			PanelBankAccounts.setEnabled(editionMode);
		}
		entryPartnerBrandName.setEnabled(editionMode);
		entryPartnerCopies.setEnabled(editionMode);
		entryPartnerCurrency.setEnabled(false);
		entryPartnerCurrentRisk.setEnabled(false);
		entryPartnerEmail.setEnabled(true);
		entryPartnerFax.setEnabled(editionMode);
		entryPartnerIdNumber.setEnabled(editionMode);
		entryPartnerMaxRisk.setEnabled(editionMode);
		entryPartnerName.setEnabled(editionMode);
		entryPartnerPartnerGroup.setEnabled(false);
		entryPartnerPaymentMethod.setEnabled(false);
		entryPartnerPhone1.setEnabled(editionMode);
		entryPartnerPhone2.setEnabled(editionMode);
		entryPartnerRappelType.setEnabled(false);
		entryPartnerVendor.setEnabled(false);
		entryPriceList.setEnabled(false);
		buttonCurrency.setEnabled(editionMode);
		buttonVendor.setEnabled(editionMode);
		buttonPartnerGroup.setEnabled(editionMode);
		buttonPaymentMethod.setEnabled(editionMode);
		buttonPriceList.setEnabled(editionMode);
		buttonRappelType.setEnabled(editionMode);
		comboVatRegime.setEnabled(editionMode);
		buttonPartnerIdNumberCompute.setEnabled(editionMode);
		extCatalog.setEnabled(editionMode);
		entryPartnerEmail.setEnabled(editionMode);
		entryPromptPaymentDiscount.setEnabled(editionMode);
		entryFixedDiscount.setEnabled(editionMode);

	}

	public void on_compute_id_number_button_clicked(Event e) {

		String dni = entryPartnerIdNumber.getText();

		if (Validator.getInstance().isValidSpanishCif(dni)) {
			ExplicaGUIBuilder.getInstance().showConfirmationDialog(
					this.entEditView.getGui().getParentWindow().getShell(),
					"Es un CIF válido", "Atención", SWT.ICON_INFORMATION,
					SWT.OK);
			return;
		}
		if (Validator.getInstance().isValidSpanishNif(dni)) {
			ExplicaGUIBuilder.getInstance().showConfirmationDialog(
					this.entEditView.getGui().getParentWindow().getShell(),
					"Es un NIF válido", "Atención", SWT.ICON_INFORMATION,
					SWT.OK);
			return;
		}

		if (!Validator.getInstance().isValidDNI(dni))
			ExplicaGUIBuilder.getInstance().showConfirmationDialog(
					this.entEditView.getGui().getParentWindow().getShell(),
					"DNI no válido", "Atención", SWT.ICON_ERROR, SWT.OK);
		else {

			char letra = Converter.getInstance().letraNIF(dni);
			if (letra != 0) {
				StringBuilder sb = new StringBuilder(dni);
				sb.append(letra);
				entryPartnerIdNumber.setText(sb.toString());

			}
		}

	}

	@Override
	public void buildSelectionSets() {

		lSelectionSubentities = new HashSet<SelectionSubentityOT>();

		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_PARTNERGROUP_ID,
				InterfacePartners.EDIT_PARTNER_PARTNERGROUP_WIDGETNAME, this,
				buttonPartnerGroup));
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_PAYMENTMETHOD_ID,
				InterfacePartners.EDIT_PARTNER_PAYMENTMETHOD_WIDGETNAME, this,
				buttonPaymentMethod));
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfaceExplica.EXPLICA_ENTITY_CURRENCY_ID,
				InterfacePartners.EDIT_PARTNER_CURRENCY_WIDGETNAME, this,
				buttonCurrency));
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_RAPPELTYPE_ID,
				InterfacePartners.EDIT_PARTNER_RAPPELTYPE_WIDGETNAME, this,
				buttonRappelType));
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_VENDOR_ID,
				InterfacePartners.EDIT_PARTNER_VENDOR_WIDGETNAME, this,
				buttonVendor));

		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfaceExplica.EXPLICA_ENTITY_PRICELIST_ID,
				InterfacePartners.EDIT_PARTNER_PRICELIST_WIDGETNAME, this,
				buttonPriceList));

		lMultiSelectionSubentities = new HashSet<MultiSelectionSubentityOT>();

		lMultiSelectionSubentities.add(new MultiSelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_CONTACT_ID, "treePartnerContacts", this,
				this.getSwtWidget(InterfacePartners.EDIT_PARTNER_CONTACT_PANEL_WIDGETNAME), "contactSet", "partner"));

		lMultiSelectionSubentities.add(new MultiSelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_ADDRESS_ID, "treePartnerAddresses", this,
				this.getSwtWidget(InterfacePartners.EDIT_PARTNER_ADDRESS_PANEL_WIDGETNAME), "addressSet", null));

		lMultiSelectionSubentities.add(new MultiSelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_BANKACCOUNT_ID, "treePartnerBankAccounts",
				this, this.getSwtWidget(InterfacePartners.EDIT_PARTNER_BANKACCOUNT_PANEL_WIDGETNAME), "bankAccountSet", "partner"));

	}

	@Override
	public void init_specific_components() {
		ExplicaGUIBuilder.getInstance().connectSignal(
				InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED,
				buttonPartnerIdNumberCompute,
				"on_compute_id_number_button_clicked", this);

	}

	@Override
	public void build_extension_fields_view(){
		this.extFieldsView = new ExplicaExtensionFieldsView(this.entEditView.getGui());
		extFieldsView.setExtCatalog(extCatalog);
		extFieldsView.setLogger(logger);
		extFieldsView.setEntityLauncher(this);
		
	}
	
	@Override
	public OperationResult printEntity(){
		OperationResult result=new OperationResult();
		String reportPath=InterfacePartners.EXPLICA_PARTNERS_PARTNER_REPORT;
		result.add(printEntity(reportPath, null));
		return result;
	}
	
	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((PartnerOT) selectedEntity);
		}
	}
}
